//
//  ForgetPwdViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/13.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "ReSetPwdViewController.h"

@interface ForgetPwdViewController ()
@property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (weak, nonatomic) IBOutlet UIButton *CodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *SubmitBtn;
@property (weak, nonatomic) IBOutlet UIView *FirstLabel;
@property (weak, nonatomic) IBOutlet UIView *SecondLabel;

@property (assign, nonatomic) NSInteger timeNmum;
@property (strong, nonatomic) NSTimer *getTime;

@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _timeNmum = 60;
    _PhoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _CodeTF.keyboardType = UIKeyboardTypeNumberPad;
    [self createUI];
    [self judgeLoginEnabled];
}

- (void)createUI{
    self.view.backgroundColor = [UIColor whiteColor];
    _TitleLabel.layer.borderColor = [[UIColor whiteColor]CGColor];
    _TitleLabel.layer.borderWidth = 1.0f;
    [_TitleLabel.layer setCornerRadius:15.0];
    _TitleLabel.layer.masksToBounds = YES;
    
    _FirstLabel.layer.borderColor = [[UIColor colorWithHex:0xebebeb]CGColor];
    _FirstLabel.layer.borderWidth = 1.0f;
    [_FirstLabel.layer setCornerRadius:17.0];
    _FirstLabel.layer.masksToBounds = YES;
    
    _SecondLabel.layer.borderColor = [[UIColor colorWithHex:0xebebeb]CGColor];
    _SecondLabel.layer.borderWidth = 1.0f;
    [_SecondLabel.layer setCornerRadius:17.0];
    _SecondLabel.layer.masksToBounds = YES;
    
    [_SubmitBtn.layer setCornerRadius:20.0];
    _SubmitBtn.layer.masksToBounds = YES;
    
    [_CodeBtn.layer setCornerRadius:15.0];
    _CodeBtn.layer.masksToBounds = YES;
}

-(void)judgeLoginEnabled{
    
    [_PhoneTF.rac_textSignal subscribeNext:^(NSNumber *result) {
        if(String(result).length == 11){
            [_CodeBtn setBackgroundColor:[UIColor redColor]];
            _CodeBtn.enabled = YES;
        }else{
            [_CodeBtn setBackgroundColor:[UIColor grayColor]];
            _CodeBtn.enabled = NO;
        }
        
    }];
}

- (IBAction)BackClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)CodeBtnClick:(UIButton *)sender {
    if (_PhoneTF.text.length == 0 || _PhoneTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    
    [THWManager GetHttp:API(EnsurePhoneNum) Parameters:@{@"userPhone":_PhoneTF.text} Success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                [self.view loadingOnAnyView];
                _timeNmum = 60;
                NSDictionary *dic = @{@"tel":_PhoneTF.text,@"type":@"9"};
                [THWManager GetHttp:API(ForgetGetCode) Parameters:dic Success:^(id responseObject) {
                    [self.view removeAnyView];
                    if ([responseObject isKindOfClass:[NSDictionary class]]) {
                        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                            _getTime =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(begainTime) userInfo:nil repeats:YES];
                            _CodeBtn.enabled = NO;
                        }else{
                            [self.view Message:responseObject[@"msg"]];
                        }
                    }
                } andFalse:^(NSError *error) {
                    [self.view removeAnyView];
                }];
            }else{
                [self.view Message:responseObject[@"msg"]];
            }
        }
    } andFalse:^(NSError *error) {
    }];
}

- (void)begainTime
{
    if (_timeNmum > 0) {
        _timeNmum--;
        _CodeBtn.enabled = NO;
        [_CodeBtn setTitle:[NSString stringWithFormat:@"%ldS",(long)_timeNmum] forState:UIControlStateNormal];
    }else{
        _timeNmum = 0;
        if ([_getTime isValid]) {
            [_getTime invalidate];
            _getTime = nil;
        }
        [_CodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        _CodeBtn.enabled = YES;
    }
}

- (IBAction)SubmitClick:(UIButton *)sender {
    if (_PhoneTF.text.length == 0 || _PhoneTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    if (_CodeTF.text.length == 0) {
        [self.view Message:@"请输入验证码" HiddenAfterDelay:1];
        return;
    }
    [self.view loadingOnAnyView];
    NSDictionary *dic = @{@"userPhone":_PhoneTF.text,@"phone_codes":_CodeTF.text};
    [THWManager GetHttp:API(ForgetPSW) Parameters:dic Success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [self.view removeAnyView];
            if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                ReSetPwdViewController *reset = [[ReSetPwdViewController alloc] init];
                reset.phoneNum = _PhoneTF.text;
                [self.navigationController pushViewController:reset animated:YES];
            }else{
                [self.view Message:responseObject[@"msg"]];
            }
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_PhoneTF resignFirstResponder];
    [_CodeTF resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
